package com.cqemme.activitiservice.service;

import com.alibaba.fastjson.JSONObject;
import com.cqemme.activitiservice.dao.ImgDao;
import com.cqemme.activitiservice.entity.ImgEntity;
import com.cqemme.activitiservice.utils.EmptyChecker;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HospitalService {
    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    @Resource
    private ProcessEngine processEngine;

    @Resource
    private IdentityService identityService;

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private MyHistoryService historyService;

    @Autowired
    private ImgDao imgDao;

    /**
     * 患者来医院，启动流程
     *
     * @param json
     * @return
     */
    public Object patient(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String userName = reqMap.get("userName") + "";
        String time = reqMap.get("dueDate") + "";

        //医院流程key
        String proKey = "hospital";

        //处理用户身份
        Map<String, Object> var = new HashMap<>();
        var.put("userName", userName);
        var.put("nurseHandle", false);
        var.put("schoolId", "daibin");

        //挂号过期时间

        if (EmptyChecker.notEmpty(time)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dateTime = null;
            try {
                dateTime = simpleDateFormat.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            var.put("nurseDue", dateTime);
        }

        ProcessInstance pro=runtimeService.startProcessInstanceByKey(proKey, var);

        ImgEntity img = new ImgEntity();
        img.setCreateTime(new Date());
        img.setName(userName);
        img.setProId(pro.getProcessInstanceId());
        imgDao.insert(img);

        return true;
    }

    /**
     * 护士查看任务
     *
     * @param nurseName
     * @return
     */
    public List<Map<String, Object>> nurse(String nurseName) {

        List<Task> list = taskService.createTaskQuery().taskAssignee(nurseName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("dueDate", ta.getDueDate());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 护士处理任务-挂号成功与否
     *
     * @param json
     * @return
     */
    public Object nurseHandle(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        String remark = reqMap.get("remark") + "";
        boolean yes = Boolean.parseBoolean(reqMap.get("yes") + "");

        //写流程变量处理任务流向
        taskService.setVariable(taskId, "nurseHandle", yes);
        taskService.setVariable(taskId, "nurseRemark", remark);

        taskService.complete(taskId);
        return "处理成功";
    }

    /**
     * 专家查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> outpatientLike(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("dueDate", ta.getDueDate());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("nurseRemark", runtimeService.getVariable(ta.getExecutionId(), "nurseRemark"));
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 专家处理任务
     *
     * @param json
     * @return
     */
    public Object outpatientHan(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        String remark = reqMap.get("remark") + "";

        taskService.setVariable(taskId, "daibinRemark", remark);

        taskService.complete(taskId);
        return "会诊成功";
    }

    /**
     * 电子处方各类检查查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> diancheck(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("daibinRemark", runtimeService.getVariable(ta.getExecutionId(), "daibinRemark"));
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 电子处方科处理任务
     *
     * @param json
     * @return
     */
    public Object diancomit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        String remark = reqMap.get("remark") + "";
        String yaoping = reqMap.get("yaoping") + "";
        boolean beinHospital = Boolean.parseBoolean(reqMap.get("beinHospital") + "");
        double money = Double.parseDouble(reqMap.get("money") + "");

        //备注
        taskService.setVariable(taskId, "daibincheckRemark", remark);
        //判断要不要住院
        taskService.setVariable(taskId, "beinHospital", beinHospital);
        //支付的钱
        taskService.setVariable(taskId, "money", money);
        //需要用哪些药品
        taskService.setVariable(taskId, "yaoping", yaoping);


        taskService.complete(taskId);
        return "已检查";
    }

    /**
     * 缴费科查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> outmoney(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("daibincheckRemark", runtimeService.getVariable(ta.getExecutionId(), "daibincheckRemark"));
            taskMap.put("beinHospital", runtimeService.getVariable(ta.getExecutionId(), "beinHospital"));
            taskMap.put("money", runtimeService.getVariable(ta.getExecutionId(), "money"));
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 缴费科完成任务
     *
     * @param json
     * @return
     */
    public Object moneycomit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";

        taskService.complete(taskId);
        return "缴费成功";
    }

    /**
     * 药房查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> yaofang(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            taskMap.put("yaoping", runtimeService.getVariable(ta.getExecutionId(), "yaoping"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 药房完成任务
     *
     * @param json
     * @return
     */
    public Object yaocommit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";

        taskService.complete(taskId);
        return "拿药成功";
    }

    /**
     * 住院部查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> zhuyuanlike(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            taskMap.put("daibincheckRemark", runtimeService.getVariable(ta.getExecutionId(), "daibincheckRemark"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 住院部安排住院完成任务
     *
     * @param json
     * @return
     */
    public Object zhuyuancommit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        //开启定时任务，需要专家继续会诊
        taskService.setVariable(taskId, "goMedical", false);
        //开启定时任务，需要药物继续治疗
        taskService.setVariable(taskId, "drug", false);
        //手术还没做
        taskService.setVariable(taskId, "surgery", false);

        taskService.complete(taskId);
        return "住院受理成功";
    }

    /**
     * 手术部门查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> shoushulike(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            taskMap.put("daibincheckRemark", runtimeService.getVariable(ta.getExecutionId(), "daibincheckRemark"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 手术治疗完成任务
     *
     * @param json
     * @return
     */
    public Object shoushucommit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        double money = Double.parseDouble(reqMap.get("money") + "");
        //手术完成
        taskService.setVariable(taskId, "surgery", true);
        //此次手术花费的钱
        money += Double.parseDouble(taskService.getVariable(taskId, "money") + "");
        taskService.setVariable(taskId, "money", money);

        taskService.complete(taskId);
        return "手术成功";
    }

    /**
     * 定时专家科查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> timezhuanjialike(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            taskMap.put("daibincheckRemark", runtimeService.getVariable(ta.getExecutionId(), "daibincheckRemark"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 定时专家科完成任务
     *
     * @param json
     * @return
     */
    public Object timezhuanjiacommit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        double money = Double.parseDouble(reqMap.get("money") + "");
        //此次专家科看病花费的钱
        money += Double.parseDouble(taskService.getVariable(taskId, "money") + "");
        taskService.setVariable(taskId, "money", money);
        try {
            boolean goMedical = Boolean.parseBoolean(reqMap.get("goMedical") + "");
            if (goMedical == true) {
                taskService.setVariable(taskId, "goMedical", true);
            }
        } catch (Exception e) {
        }

        taskService.complete(taskId);
        return "此次会诊处理成功";
    }

    /**
     * 定时药房科查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> timeyaofanglike(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            taskMap.put("daibincheckRemark", runtimeService.getVariable(ta.getExecutionId(), "daibincheckRemark"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 定时药房科完成任务
     *
     * @param json
     * @return
     */
    public Object timeyaofangcommit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";
        double money = Double.parseDouble(reqMap.get("money") + "");
        //此次拿药花费的钱
        money += Double.parseDouble(taskService.getVariable(taskId, "money") + "");
        taskService.setVariable(taskId, "money", money);
        try {
            boolean drug = Boolean.parseBoolean(reqMap.get("drug") + "");
            if (drug == true) {
                taskService.setVariable(taskId, "drug", true);
            }
        } catch (Exception e) {
        }

        taskService.complete(taskId);
        return "处理成功";
    }

    /**
     * 住院结算科查看任务
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> clearmoneylike(String userName) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(userName).list();
        List<Map<String, Object>> reList = new ArrayList();
        for (Task ta : list) {
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("name", ta.getName());
            taskMap.put("createTime", ta.getCreateTime());
            taskMap.put("executionId", ta.getExecutionId());
            taskMap.put("id", ta.getId());
            taskMap.put("processInstanceId", ta.getProcessInstanceId());
            taskMap.put("userName", runtimeService.getVariable(ta.getExecutionId(), "userName"));
            taskMap.put("money", runtimeService.getVariable(ta.getExecutionId(), "money"));
            reList.add(taskMap);
        }
        return reList;
    }

    /**
     * 住院结算科完成任务
     *
     * @param json
     * @return
     */
    public Object clearmoneycommit(String json) {
        Map<String, Object> reqMap = JSONObject.parseObject(json, Map.class);
        String taskId = reqMap.get("taskId") + "";

        taskService.complete(taskId);
        return "出院办理成功";
    }

}
